package org.clob4eclipse.wizards;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.clob4eclipse.Config;
import org.clob4eclipse.PluginData;
import org.clob4eclipse.PluginLogger;



public class DbSelectionListBean {

	Object[] listArray = null;
	PluginData data = null;
	String query = null;
	
	
	public DbSelectionListBean(PluginData data, String query){
		this.data = data;
		this.query = query;
		init();
	}
	
	public void init(){
		ResultSet rs = null;
		Config config = null;
		List<Object> list = new ArrayList<Object>(); 
		
		try {
			config = new Config(data);
			rs = config.getStatement().executeQuery(query);
			while (rs.next()) {
				list.add(rs.getString(1));
			}
		} catch (Exception e) {
			PluginLogger.logError("Failed to retieve selection list: ", e);
		} finally {
			try {
				if (config.getStatement() != null) config.getStatement().close();	
				if (config.getConnection() != null)	config.getConnection().close();
			} catch (SQLException e) {
				PluginLogger.logError("Failed to close Connection: ", e);
			}
		}
		
		listArray = list.toArray();
	}
	
	public Object[] getObjectArray(){
		return listArray;
	}
	
	public String getEntry(int number) {
		if (listArray != null && listArray.length >= number) {
			PluginLogger.logWarning("number in getEntry "+number+"  ..  "+listArray);
			return listArray[number].toString();
		}
		return null;
	}	
}
